package com.aerozhonghuan.logisticsmall.modules.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.aerozhonghuan.foundation.base.BaseFragment;
import com.aerozhonghuan.foundation.eventbus.EventBusManager;
import com.aerozhonghuan.foundation.log.LogUtil;
import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.modules.common.event.UserInfoChangedSuccessEvent;
import com.aerozhonghuan.logisticsmall.modules.home.beans.ActivityListBean;
import com.aerozhonghuan.logisticsmall.modules.home.logic.HomePageInfoRequest;
import com.aerozhonghuan.logisticsmall.modules.home.search.MKeyWordSearchActivity;
import com.aerozhonghuan.logisticsmall.modules.home.search.SearchAcrivity;
import com.aerozhonghuan.logisticsmall.widget.DrawableCenterTextView;
import com.aerozhonghuan.logisticsmall.widget.ProgressDialogIndicator;
import com.aerozhonghuan.logisticsmall.widget.TitleBarView;
import com.aerozhonghuan.oknet2.CommonCallback;
import com.aerozhonghuan.oknet2.CommonMessage;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * 描述: HomeFragment
 * 作者:zhangyonghui
 * 创建日期：2017/6/20  on 上午 11:27
 */
public class HomeFragment extends BaseFragment implements View.OnClickListener {

    private static final String TAG = "HomeFragment";
    private View rootView;
    private TitleBarView titleBar;
    private ProgressDialogIndicator dialog;
    private DrawableCenterTextView mSearchView;
    private ListView mCategoryListVIew;
    private HomePageProductListAdapter mListAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBusManager.register(this);
        dialog = new ProgressDialogIndicator(getActivity());
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_home, null);
            initView();
            initData();
        }
        return rootView;
    }

    private void initView() {
        titleBar = (TitleBarView) rootView.findViewById(R.id.titlebarview1);
        titleBar.enableBackButton(false);
        titleBar.setTitle(getResources().getString(R.string.title_activity_main));
        mSearchView = (DrawableCenterTextView) rootView.findViewById(R.id.search_title);
        mSearchView.setOnClickListener(this);
        mSearchView.setFocusable(false);

        mCategoryListVIew = (ListView) rootView.findViewById(R.id.my_category_list);
        mListAdapter = new HomePageProductListAdapter(getActivity());
        mCategoryListVIew.setAdapter(mListAdapter);
    }

    private void initData() {
        HomePageInfoRequest.activityList(getActivity(), dialog, new CommonCallback<ActivityListBean>() {
            @Override
            public void onSuccess(ActivityListBean messsageBodyObject, CommonMessage commonMessage, String allResponseString) {
                mListAdapter.setData(messsageBodyObject);
            }
        });
    }


    @Override
    public void onDestroy() {
        EventBusManager.unregister(this);
        super.onDestroy();
    }

    @Override
    public void onStart() {
        super.onStart();
    }


    /**
     * 显示隐藏fragment时控制轮播
     *
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {

        } else {

        }
    }


    /**
     * 用户信息发生改变
     *
     * @param e
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    @SuppressWarnings("unused")
    public void onEvent(UserInfoChangedSuccessEvent e) {
        if (getActivity() == null || getActivity().isFinishing())
            return;
        LogUtil.d(TAG, "收到事件: 用户信息发生改变");

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.search_title:
                intent.setClass(getActivity(), SearchAcrivity.class);
                startActivity(intent);
                break;
        }
    }
}
